# -*- coding: utf-8 -*-
from pgmagick import Image
import cv2,os

class Binary:
    def __init__(self):
        self.raw="./raw"
        self.res = "./res"
        # bigger,blacker
        self.thresh=88

    def get_all(self,dir):
        l=[]
        for i in os.listdir(dir):
            f=os.path.join(dir,i)
            l.append(os.path.abspath(f))
        return l

    def sharpen(self,path):
        img = Image(path)
        img.sharpen(2)
        f=os.path.basename(path)
        res=os.path.join(self.res,f)
        img.write(res)
    def binary(self,path):
        f = os.path.basename(path)
        res = os.path.join(self.res, f)
        im = cv2.imread(path)
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        (thresh, im0_255) = cv2.threshold(gray, self.thresh, 255, cv2.THRESH_BINARY)
        cv2.imwrite(res,im0_255)
    def run(self):
        for path in self.get_all(self.raw):
            self.sharpen(path)
        for path in self.get_all(self.res):
            print (path)
            self.binary(path)

if __name__ == '__main__':
    b=Binary()
    b.run()
